package trading.ctm.yunjing.module.login.activity

import android.content.Intent
import android.os.Bundle
import cc.vv.baselibrary.activity.BaseActivity
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.lkbasecomponent.util.LKStringUtil
import kotlinx.android.synthetic.main.activity_regist.*
import trading.ctm.yunjing.R
import trading.ctm.yunjing.util.*


/**
 * @desc 注册
 * @author zouxiaorui
 * @date  2018/8/8
 */
class RegistActivity : BaseActivity() {

    private var mPhoneNumber = ""
    private var mCode = ""
    private var mPassword = ""
    override fun initLayoutRes(): Int {
        return R.layout.activity_regist
    }

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)
        btbv_ar_regist.setWhetherShowDividerView(false)
    }

    override fun initAction(data: Bundle?) {
        super.initAction(data)

        btbv_ar_regist.setEventInterface(object : BaseTopBarView.EventInterface{
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {
            }

            override fun rightOnClick() {
            }

        })

        //手机号
        phoneChange(tiet_ar_phone_number, tiet_ar_code, tiet_ar_password, iv_ar_checkout_phone, tv_ar_regist)
        //验证码
        codeChange(tiet_ar_code, tiet_ar_phone_number, tiet_ar_password, tv_ar_regist)
        //密码
        passwordChange(tiet_ar_password, tiet_ar_phone_number, tiet_ar_code, tv_ar_regist)

        //获取验证码
        tv_ar_obtain_code.setOnClickListener {
            mPhoneNumber = tiet_ar_phone_number.text.toString()
            obtainCode(mPhoneNumber)
        }
        //注册
        tv_ar_regist.setOnClickListener {
            mPhoneNumber = tiet_ar_phone_number.text.toString()
            mCode = tiet_ar_code.text.toString()
            mPassword = tiet_ar_password.text.toString()
            if (isPhoneNumber(mPhoneNumber) && isCode(mCode) && isPassword(mPassword)) {
                regist(mPhoneNumber, mCode, mPassword)
            }else if(!isPhoneNumber(mPhoneNumber)){
                LKToastUtil.showToastShort("请输入正确手机号码")
            }else if(!isPassword(mPassword)){
                LKToastUtil.showToastShort("密码格式不正确")
            }else if(!isCode(mCode)){
                LKToastUtil.showToastShort("请输入正确验证码")
            }
        }


    }

    private fun obtainCode(phone: String) {
        if (LKStringUtil.isPhoneNumber(phone)) {
            countDownTime(tv_ar_obtain_code, 60000L, "s")
        } else {
            LKToastUtil.showToastShort("请输入正确手机号码")
        }
    }

    private fun regist(phone: String, code: String, password: String) {
        startActivity(Intent(this@RegistActivity, RegistSuccessActivity::class.java))
    }

}
